﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media;

namespace Mota
{
    public partial class WeatherPage : PhoneApplicationPage
    {

        public class WeatherItem
        {
            //屬性要與ItemTemplate中Data Binding定義的相同
            public string WeatherIcon { get; set; }
            public string WeatherStation { get; set; }
            public string temperature { get; set; }
            public string windDir { get; set; }
            public string Rainfall { get; set; }
        }

        List<WeatherItem> tCollection = new List<WeatherItem>();        // Weather data collection

        httpUtil myHttpUtil;
        public WeatherPage()
        {
            InitializeComponent();
            GetWeatherData();
            LastUpdateTime();                                                               // updata time
            
        }

        void GetWeatherData()
        {
            myHttpUtil = new httpUtil();
            myHttpUtil.testRequest("http://mota.taipei.gov.tw/mota2/Weather.ashx");         // get data ( json )
        }
        
        /// <summary>
        /// 回前一頁，onclick Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Back_Click(object sender, RoutedEventArgs e)
        {
            ((PhoneApplicationFrame)Application.Current.RootVisual).Content = new MainPage();        // new page            
        }

        /// <summary>
        /// 立即更新，onclick Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WeatherUpdate_Click(object sender, RoutedEventArgs e)
        {
            LastUpdateTime();
            UpdateData();
        }

        /// <summary>
        /// 處理按下"立即更新"後，最後更新的時間
        /// </summary>
        void LastUpdateTime()
        {
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;

            updateTime.Text = "最後更新時間：" + currentTime.Year.ToString() + @"-" + currentTime.Month.ToString("00") + @"-" + currentTime.Day.ToString("00") + "  " + currentTime.Hour.ToString("00") + ":" + currentTime.Minute.ToString("00");
        }

        /// <summary>
        /// 更新氣象資料
        /// </summary>
        bool isData = false;
        void UpdateData()
        {
            //GetWeatherData();

            if (isData)                 // 假如有資料，清除舊資料
                tCollection.Clear();

            foreach (var res in myHttpUtil.weatherList)
            {
                WeatherItem _item = new WeatherItem();
                int iconNum = Int32.Parse(res.weather_icon);
                
                if(iconNum != 99)
                {
                    _item.WeatherIcon = "/Mota;component/Images/weather_" + iconNum + ".png";       // weather icon
                }
                _item.WeatherStation = res.station;                                                 // weather station
                _item.windDir = "風向：" + res.wind_dir;                                            // wind direction
                _item.temperature = "氣溫：" + res.temperature;                                    // current temperature
                _item.Rainfall = "累積雨量：" + res.accumulated_prec;                                //current accumulated rainfall
                
                tCollection.Add(_item);
            }

            isData = true;
            myListBox.ItemsSource = tCollection;
            
        }
    }
}
